package com.ithuangyl.common.mvp.base;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.ithuangyl.common.base.BaseActivity;
import com.ithuangyl.common.mvp.factory.IPresenterFactory;
import com.ithuangyl.common.mvp.factory.PresenterFactoryImpl;
import com.ithuangyl.common.mvp.proxy.BaseMvpPresenterProxyImpl;
import com.ithuangyl.common.mvp.proxy.IPresenterProxy;

public abstract class BaseMvpActivity<V extends IBaseMvpView, P extends BaseMvpPresenter<V>> extends BaseActivity implements IPresenterProxy<V, P> {
    private static final String PRESENTER_SAVE_KEY = "presenter_save_key";

    /**
     * 创建被代理对象，传入默认Presenter的工厂
     */
    private BaseMvpPresenterProxyImpl<V, P> mProxy = new BaseMvpPresenterProxyImpl<>(PresenterFactoryImpl.<V, P>createFactory(getClass()));
    protected Activity activity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (null != savedInstanceState) {
            mProxy.onRestoreInstanceState(savedInstanceState.getBundle(PRESENTER_SAVE_KEY));
        }
        mProxy.onCreate((V) this);
        initData(savedInstanceState);
    }

    @Override
    public void setPresenterFactory(IPresenterFactory<V, P> presenterFactory) {
        mProxy.setPresenterFactory(presenterFactory);
    }

    @Override
    public IPresenterFactory<V, P> getPresenterFactory() {
        return mProxy.getPresenterFactory();
    }

    @Override
    public P getPresenter() {
        return mProxy.getPresenter();
    }

    /**
     * 获取Layout布局
     *
     * @return
     */
//    public abstract int getContentId();

    /**
     * 初始化数据
     *
     * @param savedInstanceState
     */
    public  void initData(Bundle savedInstanceState){};

    /**
     * @param outState
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle(PRESENTER_SAVE_KEY, mProxy.onSaveInstanceState());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mProxy.onDestroy();
    }
}
